package com.kiss.fw.servlets;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.kiss.fw.utils.Constants;
import com.kiss.fw.utils.Util;

public abstract class AbstractServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(AbstractServlet.class);
	private String servletName;
	
	protected AbstractServlet() {
	}
	
	protected abstract void initialize() throws Exception;
	protected abstract void cleanUp() throws Exception;
	protected abstract void executeDelete(HttpServletRequest _request, HttpServletResponse _response) throws Exception;
	protected abstract void executeGet(HttpServletRequest _request, HttpServletResponse _response) throws Exception;
	protected abstract void executeHead(HttpServletRequest _request, HttpServletResponse _response) throws Exception;
	protected abstract void executeOptions(HttpServletRequest _request, HttpServletResponse _response) throws Exception;
	protected abstract void executePost(HttpServletRequest _request, HttpServletResponse _response) throws Exception;
	protected abstract void executePut(HttpServletRequest _request, HttpServletResponse _response) throws Exception;
	protected abstract void executeTrace(HttpServletRequest _request, HttpServletResponse _response) throws Exception;
	
	private void handleExecuteException(final String _message, Exception _exception) throws ServletException {
		Util.handleException(logger, _message, _exception);
		
		// throw singleton servlet exception
		throw Constants.Exceptions.SERVLET_EXCEPTION;
	}
	
	public final void destroy() {
		logger.info("destroy() - " +servletName);
		
		try {
			cleanUp();
		}
		catch(Exception _exception) {
			Util.handleException(logger, "destroy() - Error", _exception);
		}
	}
	
	public final void init(final ServletConfig _config) throws ServletException {
		servletName = _config.getServletName();
		logger.info("init() - " +_config.getServletName());
		
		try {
			initialize();
		}
		catch(Exception _exception) {
			handleExecuteException("init() - Error", _exception);
		}
	}
	
	protected final void doDelete(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
		try {
			executeDelete(_request, _response);
		}
		catch(Exception _exception) {
			handleExecuteException("doDelete() - Error", _exception);
		}
	}

	protected final void doGet(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
		try {
			executeGet(_request, _response);
		}
		catch(Exception _exception) {
			handleExecuteException("doGet() - Error", _exception);
		}
	}
	
	protected final void doHead(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
		try {
			executeHead(_request, _response);
		}
		catch(Exception _exception) {
			handleExecuteException("doHead() - Error", _exception);
		}
	}
	
	protected final void doOptions(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
		try {
			executeOptions(_request, _response);
		}
		catch(Exception _exception) {
			handleExecuteException("doOptions() - Error", _exception);
		}
	}
	
	protected final void doPost(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
		try {
			executePost(_request, _response);
		}
		catch(Exception _exception) {
			handleExecuteException("doPost() - Error", _exception);
		}
	}
	
	protected final void doPut(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
		try {
			executePut(_request, _response);
		}
		catch(Exception _exception) {
			handleExecuteException("doPut() - Error", _exception);
		}
	}
	
	protected final void doTrace(HttpServletRequest _request, HttpServletResponse _response) throws ServletException, IOException {
		try {
			executeTrace(_request, _response);
		}
		catch(Exception _exception) {
			handleExecuteException("doTrace() - Error", _exception);
		}
	}
}